namespace ScrumSprintMonitor.ServerAdapter.TFS.TemplateAdaptors
{
	#region #using Directives

	using Infrastructure;

	using Isolation.TeamFoundationServer;

	#endregion

	public class ConchangoScrumWorkItemAdapter : CoreWorkItemAdapter
	{
		public ConchangoScrumWorkItemAdapter(IWorkItem workItem)
			: base(workItem)
		{
		}

		protected override void RetrieveState()
		{
			switch (WorkItem.State)
			{
				case "Deleted":
				case "Deferred":
				case "Ready For Test":
				case "Done":
					State = WorkItemState.Done;
					break;
				case "In Progress":
					State = WorkItemState.InProgress;
					break;
				default:
					State = WorkItemState.NotDone;
					break;
			}
		}

		protected override void RetrieveDoneWork()
		{
			var estimatedEffort = WorkItem["Conchango.TeamSystem.Scrum.EstimatedEffort"];
			DoneWork = (estimatedEffort != null ? (double) estimatedEffort - RemainingWork : 0.0);
		}

		protected override void RetrieveRemainingWork()
		{
			if (WorkItem.State == "Deleted")
			{
				RemainingWork = 0.0;
			}
			else
			{
				var remainingWork = WorkItem["Conchango.TeamSystem.Scrum.WorkRemaining"];

				RemainingWork = (remainingWork == null ? double.NaN : (double) remainingWork);
			}
		}

		protected override void RetrieveType()
		{
			switch (WorkItem.Type)
			{
				case "Sprint Backlog Item":
					Type = WorkItemType.SprintBacklogItem;
					break;
				case "Product Backlog Item":
					Type = WorkItemType.UserStory;
					break;
				case "Bug":
					Type = WorkItemType.Defect;
					break;
				default:
					Type = WorkItemType.Unknown;
					break;
			}
		}

		protected override void RetrieveTeam()
		{
			Team = (string) WorkItem["Conchango.TeamSystem.Scrum.Team"];
		}
	}
}